#!/bin/bash

# Flutter项目快速测试脚本
# 用于快速验证代码更改

set -e

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m'

# 项目根目录
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$PROJECT_ROOT"

# 计时
START_TIME=$(date +%s)

echo -e "${BLUE}🚀 开始快速测试...${NC}"
echo -e "${YELLOW}项目路径: $PROJECT_ROOT${NC}"

# 验证baseUrl配置
echo -e "${BLUE}⚙️  验证baseUrl配置...${NC}"
HTTP_FILE="larapress_app_package/common/lib/export/http.dart"
if [ -f "$HTTP_FILE" ]; then
    BASE_URL=$(grep -o "baseUrl: '[^']*'" "$HTTP_FILE" | sed "s/baseUrl: '\([^']*\)'/\1/")
    
    if [ -n "$BASE_URL" ] && [[ "$BASE_URL" =~ ^https?:// ]]; then
        echo -e "${GREEN}✓${NC} baseUrl验证通过: $BASE_URL"
    else
        echo -e "${YELLOW}⚠${NC} baseUrl配置可能有问题"
    fi
else
    echo -e "${YELLOW}⚠${NC} HTTP配置文件不存在: $HTTP_FILE"
fi

# 获取依赖（如果需要）
if [ ! -d "build" ]; then
    echo -e "${BLUE}📦 获取依赖...${NC}"
    flutter pub get
fi

# 代码分析
echo -e "${BLUE}🔍 代码分析...${NC}"
flutter analyze

# 运行测试
echo -e "${BLUE}🧪 运行测试...${NC}"
flutter test

# 格式化检查
echo -e "${BLUE}✨ 格式化检查...${NC}"
dart format --set-exit-if-changed lib/ test/ || {
    echo "自动格式化代码..."
    dart format lib/ test/
}

# 计算耗时
END_TIME=$(date +%s)
DURATION=$((END_TIME - START_TIME))

echo -e "${GREEN}✅ 快速测试完成！${NC}"
echo -e "${YELLOW}总耗时: ${DURATION}秒${NC}"
